package com.sj.uabrowser.mvp.video.presenter;

import com.orhanobut.logger.Logger;
import com.sj.uabrowser.mvp.video.bean.NVideoItem;
import com.sj.uabrowser.mvp.video.bean.NVideoTab;
import com.sj.uabrowser.mvp.video.contract.VideosContract;
import com.sj.uabrowser.mvp.video.bean.VideoNav;
import com.sj.uabrowser.mvp.video.bean.VideoTab;
import com.sj.uabrowser.mvp.video.loader.VideoListLoader;
import com.sj.uabrowser.net.bean.NewUrl;

import java.util.List;
import java.util.TreeMap;


public class VideosPresenter implements VideosContract.Presenter {

    VideoListLoader videoListLoader;
    VideosContract.View mView;
    int position;

    public VideosPresenter(VideosContract.View mView) {
        videoListLoader = new VideoListLoader(this);
        this.mView = mView;
    }

    @Override
    public void fetchTabVideoList(TreeMap<String, Object> params) {
        if(videoListLoader!=null) {
            videoListLoader.fetchTabVideoList(2, params);
        }
    }

    long time ;

    @Override
    public void fetchVideos(TreeMap<String, Object> params) {
        if(videoListLoader!=null) {
            time = System.currentTimeMillis();
            videoListLoader.fetchVideoList(0, params);
        }
    }

    @Override
    public void refreshVideos(TreeMap<String, Object> params) {
        if(videoListLoader!=null) {
            time = System.currentTimeMillis();
            videoListLoader.refreshVideoList(0, params);
        }
    }

    @Override
    public void updateVideoUrl(TreeMap<String, Object> params,int position) {
        this.position = position;
        if(videoListLoader!=null) {
            time = System.currentTimeMillis();
            videoListLoader.fetchVideoApars(1, params);
        }
    }

    @Override
    public void onDataCallBack(int requestId, Object data) {
        if(mView==null)return;
        switch (requestId) {
            case 0:
                time = System.currentTimeMillis()-time;
                Logger.e("time:"+time);
                mView.showVideosList((List<NVideoItem>) data);
                break;
            case 1:
                time = System.currentTimeMillis()-time;
                Logger.e(" update time:"+time);
                NewUrl newUrl = (NewUrl) data;
                mView.updateVideoUrl(newUrl.getUrl(),position);
                break;
            case 2:
                time = System.currentTimeMillis()-time;
                Logger.e("fetch tab time:"+time);
                mView.showTabVideoList((List<NVideoTab>) data);
                break;
        }
    }

    @Override
    public void onDataCallBackError(int requestId, Throwable throwable) {
       switch (requestId){
           case 0:
               time = System.currentTimeMillis()-time;
               Logger.e("timeout:"+time);
               mView.showError(throwable!=null?throwable.getMessage():"",0);
               break;
       }

    }

    @Override
    public void onDestroy() {
        if(mView!=null)mView=null;
        if(videoListLoader!=null)videoListLoader=null;
    }
}
